package com.yuandm.wxnt;



import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

import cn.jpush.android.api.JPushInterface;


public class SplashScreen extends Activity{
	

	private final int FINISH = 0;

    private ImageView mTuPian;
    private boolean isTuPian = false;
    private String TpUrl="http://nt.down.zdmdg.com/ad_pic.png";  //开启启动页广告需要png格式，现在是jpg
	

	
	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(SplashScreen.this, StartService.class);
        startService(intent);

//        JPushInterface.setAlias(SplashScreen.this,"test",
//                new TagAliasCallback() {
//
//                    @Override
//
//                    public void gotResult(int arg0, String arg1,
//
//                                          Set<String> arg2) {
//
//                        Log.i("JPush", "Jpush status: " + arg0);//状态
//
//                    }
//                });

        setContentView(R.layout.splashscreen);


        mTuPian = (ImageView)findViewById(R.id.ad_pic);
        
        new updateTp().start();




        TimerTask task = new TimerTask(){
        public void run(){
            Intent mainIntent = new Intent(SplashScreen.this, MainActivity.class);
            SplashScreen.this.startActivity(mainIntent);
            finish();
        }};

        Timer timer = new Timer();
        timer.schedule(task, 3000);


        

    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }
    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }


    @SuppressLint("HandlerLeak")
    private Handler h1 = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            switch (msg.arg1)
            {
                case FINISH:
                    mTuPian.setImageBitmap((Bitmap)msg.obj);
                    break;
            }
        }

    };


    private class updateTp extends Thread{

        @Override
        public void run() {
            Bitmap TpBody = getTuPian(TpUrl);
            //System.out.print(TpUrl);
            isTuPian = true;
            Message msg = h1.obtainMessage();
            msg.arg1 = FINISH;
            msg.obj = TpBody;
            h1.sendMessage(msg);
        }

    }
    
  
    


    public static Bitmap getTuPian(String url){
        URL myFileURL;
        Bitmap bitmap=null;
        try{
            myFileURL = new URL(url);
            HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
            conn.setConnectTimeout(6000);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        }catch(Exception e){
            e.printStackTrace();
        }

        return bitmap;

    }


}